package com.molichuxing.services.infrastructure.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.exception.BizException;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.EnterpriseBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.EnterpriseMapper;
import com.molichuxing.services.infrastructure.dto.request.create.EnterpriseCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.EnterpriseModifyDto;
import com.molichuxing.services.infrastructure.dto.response.EnterpriseDto;
import com.molichuxing.services.infrastructure.service.EnterpriseService;


/**
 * 
 * @author liyg
 * 企业用户服务实现类
 * 2020-06-02
 * 
 */
@Service("enterpriseService")
public class EnterpriseServiceImpl implements EnterpriseService{

	@Autowired
	private  EnterpriseMapper  enterpriseMapper;
	
	/**
	 * 获取企业用户详情
	 * 
	 * @param id
	 *            用户id
	 * @return
	 */
	@Override
	public EnterpriseDto getEnterprise(Integer id) {
		EnterpriseBean bean = enterpriseMapper.getById(id);
		return Convert.toEnterpriseDto(bean);
	}

	
	/**
	 * 获取企业用户列表
	 * 
	 * @param map
	 *            参数
	 * @param key:name
	 *            String 企业名称 非必填
	 * @param key:regionId
	 *            Integer 城市id 非必填
	 *@param  key:startTime
	 *            LocalDateTime 创建开始时间
	 *@param  key:endTime
	 *            LocalDateTime 创建结束时间        
	 *@param  key:sortName   
	 *            String 排序字段 
	 *@param  key:sortWay 
	 *            String 排序方式 
	 * @param key:pageNum
	 *            Integer 当前页
	 * @param key:pageSize
	 *            Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<EnterpriseDto> getEnterpriseList(Map<String, Object> map) {
		
		// 查询总条数
		long total = enterpriseMapper.getPageCount(map);
		if(total <= 0) {
			return null;
		}

		/*分页查询列表*/
		Integer pageNum = ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize = ObjectCastUtil.castInteger(map.get("pageSize"));
		List<EnterpriseBean> list = enterpriseMapper.getPage(map,pageNum,pageSize);
		List<EnterpriseDto> ListDto = Convert.toEnterpriseDto(list);
		Paged<EnterpriseDto> paged = new Paged<>(ListDto, total ,pageSize);
		
		return paged;
	}

	/**
	 * 新增企业用户信息
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(EnterpriseCreateDto createDto) {
		
		EnterpriseBean bean=enterpriseMapper.getByName(createDto.getName());
		if(null != bean) {
			throw new BizException(101, "企业名称已存在！");
		}
		bean=enterpriseMapper.getByFullName(createDto.getFullName());
		if(null != bean) {
			throw new BizException(101, "企业全称已存在！");
		}
		bean = new EnterpriseBean();
		BeanUtils.copyProperties(createDto, bean);
		int num = enterpriseMapper.create(bean);
		return num == 1;
	}
	
	
	/**
	 * 修改企业用户信息
	 * @param modify
	 * @return
	 */
	@Override
	public Boolean Modify(EnterpriseModifyDto modifyDto) {
		EnterpriseBean bean = enterpriseMapper.getById(modifyDto.getId());
		if(null == bean) {
			throw new BizException(101, "不存在该条数据！");
		}
		
		EnterpriseBean enterpriseBean=enterpriseMapper.getByName(modifyDto.getName());
		if(null != enterpriseBean) {
			if(!bean.getId().equals(enterpriseBean.getId())) {
				throw new BizException(101, "企业名称已存在！");
			}
		}
		
		enterpriseBean=enterpriseMapper.getByFullName(modifyDto.getFullName());
		if(null != enterpriseBean) {
			if(!bean.getId().equals(enterpriseBean.getId())) {
				throw new BizException(101, "企业全称已存在！");
			}
		}
		enterpriseBean = new EnterpriseBean();
		BeanUtils.copyProperties(modifyDto, enterpriseBean);
		int num = enterpriseMapper.modify(enterpriseBean);
		return num == 1;
	}
	
	/**
	 * 获取企业用户信息（精准）
	 * 
	 * @param name
	 *            企业名称
	 * @return
	 */
	@Override
	public EnterpriseDto getEnterpriseByName(String name) {
		EnterpriseBean bean=enterpriseMapper.getByName(name);
		return Convert.toEnterpriseDto(bean);
	}
	
	/**
	 * 获取企业用户信息（模糊）
	 * 
	 * @param name
	 *            企业名称
	 * @return
	 */
	@Override
	public List<EnterpriseDto> getEnterpriseFuzzyByName(String name) {
		List<EnterpriseBean> list = enterpriseMapper. getEnterpriseFuzzyByName(name);
		List<EnterpriseDto> ListDto = Convert.toEnterpriseDto(list);
		return ListDto;
	}
	
	/**
	 * 根据企业用户ids查询信息
	 * @param ids
	 * @return
	 */
	@Override
	public Map<Integer, EnterpriseDto> getEnterpriseByIds(List<Integer> ids) {
		if(null == ids || ids.isEmpty()) {
			return null;
		}
		List<EnterpriseBean> list = enterpriseMapper.getEnterpriseByIds(ids);
		if(null == list || list.isEmpty()) {
			return null;
		}
		Map<Integer, EnterpriseDto> map = Convert.toEnterpriseDto(list).stream()
		.collect(Collectors.toMap(EnterpriseDto::getId, a -> a, (k1, k2) -> k1));
		return map;
	}
}
